package Scenery;

import Scenery.BaseCharacter;
import Scenery.Thing.Thing;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.PlaneCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Plane;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Dome;
import com.jme3.scene.shape.Quad;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;
import com.jme3.util.TangentBinormalGenerator;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import jme3tools.optimize.GeometryBatchFactory;
import main.Memory.Memory;
import main.Memory.Materials;

/*
 * 
 * @author: Ziden
 * 
 */

// class not made yet
public class BaseBuilder {

    private AssetManager assetManager;
    private PhysicsSpace space;
    
    public BaseBuilder(AssetManager am, PhysicsSpace sp) {
        assetManager = am;
        space = sp;
        loadMaterials();
    }

    public void loadMaterials() {
        
        Material mat_lit = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        mat_lit.setTexture("DiffuseMap", assetManager.loadTexture("Textures/face.png"));
        Memory.getTextures().addMaterial("stoneblock", "tile", assetManager.loadMaterial("Materials/pedra.j3m"));
        //Memory.getTextures().addMaterial("wall", "tile", assetManager.loadMaterial("Materials/muro.j3m"));
        Memory.getTextures().addMaterial("body", "char", assetManager.loadMaterial("Materials/Corpo.j3m"));
        Memory.getTextures().addMaterial("face","char", mat_lit);
        Memory.getTextures().addMaterial("leather","flat",assetManager.loadMaterial("Materials/couro.j3m"));
        Memory.getTextures().addMaterial("wall","flat",assetManager.loadMaterial("Materials/wall.j3m"));
        Memory.getTextures().addMaterial("box","flat",assetManager.loadMaterial("Materials/box.j3m"));
        Memory.getTextures().addMaterial("tile","grass",assetManager.loadMaterial("Materials/Grass.j3m"));
         Memory.getTextures().addMaterial("iron","flat",assetManager.loadMaterial("Materials/Iron.j3m"));
    }
    
    public void createRigidMapPhysics(Node map) {
      map.addControl(new RigidBodyControl(0));
      space.add(map);
    }
 

    public static Geometry makeBaseBlock(int x, int y, int z) {
             Quad q = new Quad(4,4);
             Geometry quad = new Geometry("quad", q);
             quad.rotate(-1.57f, 0, 0);
             quad.setMaterial(Memory.getTextures().getMaterial("stoneblock", "tile"));
             quad.setLocalTranslation(x*2,y*2,z*2);
        return quad;
    }
    
    public static Geometry makeBaseTile(int x, int y, int z,String tile) {
             Quad q = new Quad(2,2);
             Geometry quad = new Geometry("quad", q);
             quad.rotate(-1.57f, 0, 0);
             quad.setMaterial(Memory.getTextures().getMaterial("tile", "grass"));
             quad.setLocalTranslation(x*2,y,z*2);
        return quad;
    }
    
    
    public static Geometry makeBaseStone(int x, int y, int z) {
             Box q = new Box(2,2,2);
             Geometry quad = new Geometry("quad", q);
             quad.rotate(-1.57f, 0, 0);
             quad.setMaterial(Memory.getTextures().getMaterial("wall", "flat"));
             quad.setLocalTranslation(x*2+2f,y*2+2f,z*2-2f);
        return quad;
    }

    public void createThingsPhysics(ArrayList<Thing> things) {
        for(Thing thing : things) {
            space.add(thing.getCollidable());
        }
    }
    
    
    public BaseCharacter makeBaseCharacter() {
        
        BaseCharacter dude = new BaseCharacter();
        
        //head
        Sphere cabeca = new Sphere(10, 10, 0.5f);
        Geometry cabecaGeo = new Geometry("head", cabeca);
        cabeca.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres
        TangentBinormalGenerator.generate(cabeca);           // for lighting effect
        cabecaGeo.setMaterial(Memory.getTextures().getMaterial("face", "char"));
        cabecaGeo.setLocalTranslation(0, 1.6f-0.6f, 0); // Move it a bit
        cabecaGeo.rotate(1.6f, 0, 0);          // Rotate it a bit
        cabecaGeo.rotate(0, 9.5f, 0.4f);
        dude.getHeadNode().attachChild(cabecaGeo);
        dude.setHead(cabecaGeo);

        
        //body
        Sphere rock = new Sphere(10, 10, 0.5f);
        Geometry shiny_rock = new Geometry("body", rock);
        rock.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres
        TangentBinormalGenerator.generate(rock);           // for lighting effect
        shiny_rock.setMaterial(Memory.getTextures().getMaterial("body", "char"));
        shiny_rock.setLocalTranslation(0, 0.8f-0.6f, 0); // Move it a bit
        shiny_rock.rotate(1.6f, 0.3f, 0);          // Rotate it a bit
        shiny_rock.rotate(0, 0, 4f);
        dude.getBodyNode().attachChild(shiny_rock);
        dude.setBody(shiny_rock);
       
        
        // foots
        // rfoot
        Sphere foot = new Sphere(10,10, 0.3f);
        Geometry footg = new Geometry("rfoot", foot);
        footg.setMaterial(Memory.getTextures().getMaterial("leather", "flat"));
        footg.setLocalTranslation(0,-0.6f,-0.3f);
        footg.rotate(0, 4, 0);
        dude.getLegNode().attachChild(footg);
        dude.setRfoot(footg);
        
        // lfoot
        Geometry footgr = new Geometry("rfoot", foot);
        footgr.setMaterial(Memory.getTextures().getMaterial("leather", "flat"));
        footgr.setLocalTranslation(0,0-0.6f,0.3f);
        footgr.rotate(0, 4, 0);
        dude.getLegNode().attachChild(footgr);
        dude.setLfoot(footgr);

        
        //rhand
        Sphere hand = new Sphere(10,10,0.2f);
        Geometry rhand = new Geometry("rhand", hand);
        rhand.setMaterial(Memory.getTextures().getMaterial("body", "char"));
        dude.getrHandNode().setLocalTranslation(0,0.8f-0.6f,0.7f);
        rhand.rotate(0, 4, 0);
        dude.getBodyNode().attachChild(dude.getrHandNode());
        dude.getrHandNode().attachChild(rhand);
        dude.setRhand(rhand);
        
        //lhand
        Geometry lhand = new Geometry("rhand", hand);
        lhand.setMaterial(Memory.getTextures().getMaterial("body", "char"));
        dude.getlHandNode().setLocalTranslation(0,0.8f-0.6f,-0.7f);
        lhand.rotate(0, 4, 0);
        dude.getBodyNode().attachChild(dude.getlHandNode());
        dude.getlHandNode().attachChild(lhand);
        dude.setLhand(lhand);
        
        dude.getMasterBodyNode().attachChild(dude.getBodyNode());
        dude.getModel().attachChild(dude.getMasterBodyNode());
        dude.getModel().attachChild(dude.getLegNode());
        dude.getModel().attachChild(dude.getHeadNode());
        dude.loadAnimations(dude);
        
       //   DirectionalLight sun = new DirectionalLight();
      //sun.setDirection(new Vector3f(-0.5f,-5,-0.5f));
     //   sun.setColor(ColorRGBA.White);
     //   dude.getModel().addLight(sun);
        
        
       // AmbientLight al = new AmbientLight();
       // al.setColor(ColorRGBA.White);
      //  dude.getModel().addLight(al);

     
        return dude;
        
    }

    public PhysicsSpace getSpace() {
        return space;
    }

    public void setSpace(PhysicsSpace space) {
        this.space = space;
    }

    
    
}
